//import java.applet.*;
import java.awt.*;
import java.util.*;
//import java.net.*;

public class Ball
{
	

	private int pos_x;				// Variable for X - Position of the ball
	private int pos_y; 				// Variable for die Y - Position of the ball
	private int x_speed;			// Speed in w.r.t x - axis
	private int y_speed;			// Speed w.r.t to y - axis
	private int radius;				// Radius of the ball

	private int first_x;			// Start x - Position
	private int first_y;			// Start y - Position

	private int maxspeed;			// Maximum speed of the ball

	private final int x_leftout = 10;
	private final int x_rightout = 370;
	private final int y_upout = 45;
	private final int y_downout = 370;
	
	protected int randomdeno;
	protected int randomnomi;
	protected String rndfrac;
	protected int rndseed;
	
	Color color;
	
	Player player;
	
	Random rnd = new Random ();

	// Constructor
	public Ball (int radius, int x, int y, int vx, int vy, int ms, Color color, Player player, int seed)
	{
		// Initialize variables
		this.radius = radius;

		pos_x = x;
		pos_y = y;

		first_x = x;
		first_y = y;

		x_speed = vx;
		y_speed = vy;

		maxspeed = ms;
		
		rndseed = seed;
		
		this.randomdeno = rnd.nextInt(rndseed)+1;
		this.randomnomi = rnd.nextInt(rndseed)+1;
		
		while(randomdeno < randomnomi){
			this.randomdeno = rnd.nextInt(rndseed)+1;
			this.randomnomi = rnd.nextInt(rndseed)+1;
		}
		
		this.rndfrac = new String();
		this.rndfrac = this.randomnomi + "/" + this.randomdeno;
		
		System.out.println( this.rndfrac);
		

		this.color = color;

		this.player = player;
		

	}

	
	public void destroyBall(){
		this.radius = 0;
		this.randomnomi=1000;
		this.randomdeno=1;
		this.rndfrac = "";
		
	}
	public void move ()
	{
		
		pos_x += x_speed;
		pos_y += y_speed;
		isOut();
	}

	public void ballWasHit ()
	{
		//Do nothing, since we want the ball disappear.
		destroyBall();
	}
	
	

	public boolean userHit (int mouse_x, int mouse_y)
	{
		double x = mouse_x - pos_x;
		double y = mouse_y - pos_y;

		double distance = Math.sqrt ((x*x) + (y*y));

		
		if (distance < 20)
		{
			//player.addScore (10*Math.abs(x_speed) + 10);
			return true;
		}
		else return false;
	}

	private boolean isOut ()
	{
		// Ball is out from left.
		if (pos_x < x_leftout)
		{
			// The bounce changes only the velocity in x-direction.
			x_speed = -x_speed;

			return true;
		}
		// Ball is out from right
		else if (pos_x > x_rightout)
		{
			// The bounce changes only the velocity in x - direction.
			x_speed = - x_speed;

			return true;
		}
		// Ball is out from the upper border.
		else if (pos_y < y_upout)
		{
		
			// The bounce changes the velocity in y - direction.
			
			y_speed = - y_speed;
			
			return true;
		}
		// Ball is out from lower border
		else if (pos_y > y_downout)
		{
			
			//Bounce changes the velocity in y - direction.
			y_speed= - y_speed;
			
			return true;
		}
		else return false;
	}

	// 
	public void DrawBall (Graphics g)
	{
		g.setColor (color);
		g.fillOval (pos_x - radius, pos_y - radius, 2 * radius, 2 * radius);
		g.setColor(Color.white);
		g.drawString(this.rndfrac,pos_x - radius, pos_y - radius );
	}
	
	public void finalizeBall(Graphics g){
		g.finalize();
	}
	
		
	public String printBall(){
		
		String bc = color.toString();
		String temp = this.rndfrac + " "+ bc;
		
		return temp;
		
	}

	

}
